package com.iot08.api.service;

import com.iot08.api.response.BrokenScreenInsuranceResp;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.entity.app.BrokenScreenInsurance;
import com.iot08.common.entity.app.CallRecord;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/16 10:24
 **/
@Slf4j
@Service
public class BrokenScreenInsuranceService extends BaseService<BrokenScreenInsurance> {


    /**
     * @description 查询用户有效的碎屏险
     * @author cwj
     * @param [openid]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/4/16 15:35
     **/
    public ResponseMap queryBrokenScreenInsuranceInfo(String openid){
        long currentTime = System.currentTimeMillis()/1000;
        Query query = new Query();
        query.addCriteria(new Criteria().andOperator(
                Criteria.where("openid").is(openid),
                //Criteria.where("status").is(1),
                Criteria.where("startTime").lt(currentTime),
                Criteria.where("endTime").gt(currentTime)
                ));
        query.with(Sort.by(Sort.Order.desc("createTime")));
        List<BrokenScreenInsuranceResp> brokenScreenInsuranceResps = mongoTemplate.find(query,BrokenScreenInsuranceResp.class,"broken_screen_insurance");
        ResponseMap res = ResponseMap.success();
        res.put("brokenScreenInsuranceResps",brokenScreenInsuranceResps);
        return res;
    }

    /**
     * @description 创建碎屏险记录
     * @author cwj
     * @param [orderId, openid, type, currentTime, currentTimeMillis]
     * @return void
     * @date @time 2025/4/16 10:17
     **/
    public void createBrokenScreenInsurance(String orderId, String openid, Integer type, long currentTime, long currentTimeMillis,
                                            Integer status,Integer useNum,Integer serverType){
        //年会员和永久会员才送碎屏险
        if(Integer.valueOf(2).equals(type) || Integer.valueOf(3).equals(type)) {
            this.save(buildBrokenScreenInsurance(orderId,openid,type,currentTime,currentTimeMillis,status,useNum,serverType));
        }
    }

    /**
     * @description 更新用户碎屏险的状态
     * @author cwj
     * @param [orderId, openid, status]
     * @return void
     * @date @time 2025/4/16 11:07
     **/
    public void updateBrokenScreenInsuranceStatus(String orderId,String openid,Integer status) {
        Query query = new Query(Criteria.where("openid").is(openid).and("orderid").is(orderId));
        Update update = new Update();
        update.set("status",status);
        this.update(query,update);
    }

    /**
     * @description 构建对象
     * @author cwj
     * @param [orderId, openid, type, currentTime, currentTimeMillis]
     * @return com.iot08.common.entity.app.BrokenScreenInsurance
     * @date @time 2025/4/16 10:19
     **/
    private BrokenScreenInsurance buildBrokenScreenInsurance(String orderId, String openid, Integer type, long currentTime,
                                                             long currentTimeMillis,Integer status,Integer useNum,Integer serverType) {
        //创建碎屏险
        BrokenScreenInsurance brokenScreenInsurance = new BrokenScreenInsurance();
        brokenScreenInsurance.setOpenid(openid);
        brokenScreenInsurance.setOrderid(orderId);
        brokenScreenInsurance.setServerType(serverType);
        brokenScreenInsurance.setSpecificType(type);
        brokenScreenInsurance.setUseNum(useNum);
        brokenScreenInsurance.setStartTime(currentTime);
        //设置截至时间和赠送碎屏险的次数
        settingEndTimeOrNum(currentTime, type,brokenScreenInsurance);
        brokenScreenInsurance.setStatus(status);
        //设置核销码
        brokenScreenInsurance.setCheckCode(currentTimeMillis + String.valueOf((int)((Math.random()*9 +1) * 100000)));
        brokenScreenInsurance.setCreateTime(new Date());
        brokenScreenInsurance.setUpdateTime(new Date());
        return brokenScreenInsurance;
    }

    /**
     * @description 设置截至时间属性和碎屏险的赠送次数
     * @author cwj
     * @param [currentTime, type, brokenScreenInsurance]
     * @return void
     * @date @time 2025/4/16 10:19
     **/
    private void settingEndTimeOrNum(long currentTime, Integer type,BrokenScreenInsurance brokenScreenInsurance) {
        long endTime = currentTime;
        int num = 1;
        switch (type){
            case 1: {
                num = 1;
                endTime += 30*24*60*60;
                break;
            }
            case 2: {
                num = 1;
                endTime += 365*24*60*60;
                break;
            }
            case 3: {
                num = 2;
                endTime += 365*24*60*60;
                break;
            }
            default:{
                endTime += 30*24*60*60;
            }
        }
        brokenScreenInsurance.setNum(num);
        brokenScreenInsurance.setEndTime(endTime);
    }

}
